﻿using System;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using HtmlAgilityPack;

namespace 第十次作业
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private async void button1_ClickAsync(object sender, EventArgs e)
        {
            // 获取用户输入的关键字
            string keyword = textBox1.Text;

            // 异步调用Bing搜索引擎
            await GetBingSearchResults(keyword, richTextBox1);
            await GetBaiduSearchResults(keyword, richTextBox2);
        }

        private async Task GetBaiduSearchResults(string keyword, RichTextBox resultBox)
        {
            // Baidu搜索的URL
            string url = $"http://www.baidu.com/s?wd={keyword}";

            // 使用HttpClient进行网络请求
            HttpClient client = new HttpClient();

            try
            {
                string html = await client.GetStringAsync(url);
                var extractedText = ExtractBaiduResults(html);
                resultBox.Text = extractedText;
            }
            catch (HttpRequestException ex)
            {
                // 处理HTTP请求异常
                MessageBox.Show($"请求Baidu搜索时发生错误：{ex.Message}");
            }
            catch (Exception ex)
            {
                // 处理其他可能的异常
                MessageBox.Show($"发生错误：{ex.Message}");
            }
        }

        private async Task GetBingSearchResults(string keyword, RichTextBox resultBox)
        {
            // Bing搜索的URL
            string url = $"https://www.bing.com/search?q={keyword}";

            // 使用HttpClient进行网络请求
            HttpClient client = new HttpClient();
            try
            {
                string html = await client.GetStringAsync(url);
                var extractedText = ExtractBingResults(html);
                resultBox.Text = extractedText;
            }
            catch (HttpRequestException ex)
            {
                // 处理HTTP请求异常
                MessageBox.Show($"请求Bing搜索时发生错误：{ex.Message}");
            }
            catch (Exception ex)
            {
                // 处理其他可能的异常
                MessageBox.Show($"发生错误：{ex.Message}");
            }
        }

        private string ExtractBaiduResults(string html)
        {
            HtmlAgilityPack.HtmlDocument document = new HtmlAgilityPack.HtmlDocument();
            document.LoadHtml(html);

            var nodes = document.DocumentNode.SelectNodes("//div[contains(@class, 'c-abstract')] | //div[contains(@class, 'result-op')]");
            StringBuilder results = new StringBuilder();
            if (nodes != null)
            {
                foreach (var node in nodes)
                {
                    results.AppendLine(node.InnerText.Trim());
                }
            }
            return results.Length > 200 ? results.ToString().Substring(0, 199) : results.ToString();
        }

        private string ExtractBingResults(string html)
        {
            HtmlAgilityPack.HtmlDocument document = new HtmlAgilityPack.HtmlDocument();
            document.LoadHtml(html);
            var nodes = document.DocumentNode.SelectNodes("//li[@class='b_algo']//p");
            StringBuilder results = new StringBuilder();
            if (nodes != null)
            {
                foreach (var node in nodes)
                {
                    results.AppendLine(node.InnerText.Trim());
                }
            }
            return results.Length > 200 ? results.ToString().Substring(0, 199) : results.ToString();
        }
    }
}
